home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / c80tcog.lbr / PFXCMPRS.CQ / pfxcmprs.c
Text File  |  1985-08-09  |  3KB  |  129 lines

  1. /*    PROGRAM pfxcmprs;
  2.                             1982/09/25 20:34
  3.  
  4. Prefix compression of a sorted line-format file.
  5.  
  6. Copyright (C) 1982    William G. Hutchison, Jr.
  7.             P.O. Box 278
  8.             Exton, PA 19341-0278
  9.             U.S.A.
  10.  
  11.             CompuServe 70665,1307
  12.             (215) 363-7028
  13.  
  14.  
  15.     Prefix  compression  is  a simple technique for removing
  16. redundant prefixes from a sequential  line-format  file.  By
  17. line-format  file,  I  mean a file created by a text editor,
  18. with from 0  to  132  characters  on  a  line,  followed  by
  19. carriage return and line feed. Pfxcmprs reads lines from the
  20. file,  and  compares  each line with its predecessor. If the
  21. lines have from 1 to 9 characters in common at the front  of
  22. the  line (the prefix) pfxcmprs writes an ASCII digit at the
  23. front of the output line with the number of  common  digits.
  24. Otherwise,  it  simply  writes  the  line  with no preceding
  25. digit. This is based on the assumption that the  input  file
  26. has no digits at the front of its lines.
  27.     This program works best with a sorted file, for example,
  28. a spelling word list.
  29.     It  is  particularly good with a word list, because most
  30. of the redundancy in a dictionary occurs at the front of the
  31. words.
  32.  
  33.  
  34.    This program may be used freely  for  any  non-commercial
  35. purpose,  provided  that  the  user does not remove or alter
  36. this notice or the copyright statement.
  37.    Those who wish to sell  or  lease  this  program,  or  to
  38. incorporate  it  into  a  product  for sale or lease, should
  39. apply to the author (above) for licensing information.
  40.  
  41.  
  42. Disclaimer:
  43.  
  44.    This program is not covered by a warranty, either express
  45. or implied. The author shall  not  be  responsible  for  any
  46. damages  (including consequential) caused by reliance on the
  47. materials  presented,   including   but   not   limited   to
  48. typographical errors or arithmetic errors.
  49.  
  50.  
  51. Super Disclaimer:
  52.  
  53.     This  program was rejected for publication in Dr. Dobb's
  54. Journal by Assistant Editor Fritzi  Lareau  on  the  grounds
  55. that  "the algorithm was in a language which might limit its
  56. usefulness to a narrow segment of  our  readers".  You  have
  57. been warned! Use this program at your own risk!!
  58. */
  59.  
  60. #include "c80def.h"
  61. #define MAXCOMP 9
  62.  
  63. #ifdef CP_M
  64. extern FILE *STDIN, *STDOUT;
  65. static FILE *STDERR;
  66. #endif
  67.  
  68. static char    pline[MAXLINE]= EOS;
  69. static char    qline[MAXLINE]= EOS;
  70.  
  71. static char    *p= pline;
  72. static char    *q= qline;
  73.  
  74. main()    {
  75. char *t;
  76.  
  77. #ifdef CP_M
  78. STDERR= open("CON:", WRITE);
  79. #endif
  80.  
  81. putline("PFXCMPRS Copyright (C) 1982 William G. Hutchison, Jr.",
  82.     STDERR);
  83. putchar(NEWLINE);
  84. while    (getline(p))    {
  85.     put_prefix_compress(p, q);
  86.     t= p; p= q; q= t;    /* swap line pointers */
  87.     }
  88. }                /* end of main    */
  89.  
  90.  
  91. getline(s)
  92. char s[];
  93. {
  94. int i;
  95.  
  96. for    (i= 0; i < MAXLINE-3; i++)
  97.     if    ((s[i]= getchar()) == EOF)
  98.         return 0;
  99.     else if    (s[i] == NEWLINE)
  100.         break;
  101.  
  102. if    (s[i++] != NEWLINE)
  103.     s[i++]= NEWLINE;
  104. s[i]= EOS;
  105. return    i;
  106. }                /* end of getline */
  107.  
  108. put_prefix_compress(p, q)
  109. char *p, *q;
  110. {
  111. char i;
  112.  
  113. for (i= 0; i < MAXCOMP && *p == *q; i++)    {
  114.     p++; q++        /* keep scanning prefix        */;
  115.     }
  116. if    (i)
  117.     putchar(i+'0')        /* compressed, put the digit    */;
  118. putline(p, STDOUT)        /* put the line, after the prefix */;
  119. }                /* end of put_prefix_compress    */
  120.  
  121. putline(s, ofd)
  122. char *s;
  123. FILE *ofd;
  124. {
  125.  
  126. while    (*s)
  127.     putc(*s++, ofd);
  128. }                /* end of putline    */
  129.